<?php

declare(ENCODING = 'UTF-8');

/*                                                                        *
 * This script is part of the TYPO3 project - inspiring people to share!  *
 *                                                                        *
 * TYPO3 is free software; you can redistribute it and/or modify it under *
 * the terms of the GNU General Public License version 2 as published by  *
 * the Free Software Foundation.                                          *
 *                                                                        *
 * This script is distributed in the hope that it will be useful, but     *
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN-    *	
 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General      *
 * Public License for more details.                                       *
 *                                                                        */	

/**
 * @package auxo
 * @subpackage core
 * @version $Id$
 */

require_once(t3lib_extMgm::extPath('auxo') . 'core/class.tx_auxo_loader.php');
require_once(t3lib_extMgm::extPath('auxo') . 'tests/fixtures/class.tx_auxo_ComponentFactoryFixtures.php');

/**	
 * Testcase: Component Factory	
 *	
 * @package auxo
 * @subpackage core	
 * @version $Id$	
 * @copyright Copyright belongs to the respective authors
 * @author AHN
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License, version 2
 */
class  tx_auxo_ComponentFactory_testcase extends PHPUnit_Framework_testcase  {
	
	public $factory = NULL;
	public $loader = NULL;
	
	/**
	 * Sets up a test enviroment of this testcase.
	 */
	public function setUp() {
		$this->loader = new tx_auxo_loader();
		$this->factory = new tx_auxo_ComponentFactory();
	}
	
	/**
	 * Testcase: availability of factory 
	 * 
	 * @return void
	 */
	public function testcaseAvailable() {
		$this->assertTrue($this->factory instanceof tx_auxo_ComponentFactory);
	}

	/**
	 * Testcase: register and retrieve a Component
	 * 
	 * @return void
	 */
	public function testRegisterAndRetrieveComponent() {
		$object = new tx_auxo_arrayObject();
		$object->set('test', '123');
		$this->factory->register('myComponent', $object);
		$this->assertTrue($this->factory->has('myComponent'));
		$this->assertType('tx_auxo_arrayObject', $this->factory->get('myComponent'));
		$this->assertEquals('123', $this->factory->get('myComponent')->get('test'));		
	}

	/**
	 * Testcase: Load and get a singleton component
	 * 
	 * @return void
	 */
	public function testLoadAndGetSingletonComponent() {
		$this->factory->load(t3lib_extMgm::extPath('auxo') . 'tests/fixtures/myComponent');
		$this->assertTrue($this->factory->has('mySingletonComponent'));
		$this->assertType('tx_auxo_FixtureComponent', $this->factory->get('mySingletonComponent'));
	}

	/**
	 * Testcase: Load a singleton component with injected constructor arguments
	 * 
	 * @return void
	 */
	public function testConstructorInjection() {
		$this->factory->load(t3lib_extMgm::extPath('auxo') . 'tests/fixtures/myComponent');
		$this->assertSame(123, $this->factory->get('mySingletonComponent')->a);		
		$this->assertSame(456, $this->factory->get('mySingletonComponent')->b);		
	}

	/**
	 * Testcase: Load a singleton component with injected constructor value argument
	 * 
	 * @return void
	 */
	public function testConstructorInjectionWithValueArgument() {
		$this->factory->load(t3lib_extMgm::extPath('auxo') . 'tests/fixtures/myComponent');
		$this->assertSame(123, $this->factory->get('myValueComponent')->a);		
		$this->assertSame(789, $this->factory->get('myValueComponent')->b);		
	}
		
	/**
	 * Testcase: Load a singleton component with setter injection
	 *
	 * @return void
	 */
	public function testSetterInjection() {
		$this->factory->load(t3lib_extMgm::extPath('auxo') . 'tests/fixtures/myComponent');
		$this->assertSame(789, $this->factory->get('mySingletonComponent')->c);						
	}
	
	/**
	 * Testcase: load a component with a dependent component injected
	 *
	 * @return void
	 */
	public function testGetComponentWithInjectDependentComponent() {
		$this->factory->load(t3lib_extMgm::extPath('auxo') . 'tests/fixtures/myComponent');
		$this->assertTrue($this->factory->has('myMainComponent'));
		$this->assertTrue($this->factory->has('myDependentComponent'));
		$component = $this->factory->get('myMainComponent');
		$this->assertType('tx_auxo_FixtureComponent', $component);
		$this->assertType('tx_auxo_FixtureDependentComponent', $component->getDependent());
		$this->assertEquals('abcd', $component->getDependent()->d);
	}

	/**
	 * Testcase: load a component with a hash argument injected
	 * 
	 * @test
	 */
	public function testGetComponentWithHashArgumentsInjected() {
		$this->factory->load(t3lib_extMgm::extPath('auxo') . 'tests/fixtures/myComponent');
		$this->assertTrue($this->factory->has('FixtureHashComponent'));
		$component = $this->factory->get('FixtureHashComponent');
		$this->assertType('tx_auxo_FixtureHashComponent', $component);
		$this->assertType('tx_auxo_HashArray', $component->hash);
		debug($component->hash);
		$this->assertEquals(3, count($component->hash));
		$this->assertTrue($component->hash->has('Berlin'));
		$this->assertTrue($component->hash->has('Hamburg'));
		$this->assertTrue($component->hash->has('Paris'));		
	}

	/**
	 * Testcase: load a component with a collection argument injected
	 *
	 * @return void
	 */
	public function testGetComponentWithCollectionArgumentsInjected() {
		$this->markTestIncomplete(
          'This test has not been implemented yet.'
        );
	}
	
	/**
	 * Tears down a created test enviroment of this testcase.
	 */
	public function tearDown() {
		unset($this->factory);
		unset($this->loader);
	}
}